package thread;

public class SynchronizedTest {
	static int[] arr = new int[6];
	static Object lock = new Object();
	public static void main(String[] args) {
		test();
	}

	private static void test() {
		class T1 extends Thread{
			@Override
			public void run() {
				int number=1;
				while(true){
					synchronized(lock){//加锁写入连续，不加锁不连续
						for (int i = 0; i < 6; i++) {
							arr[i]=number++;
						}
					}
					
				}
			}
		}
		
		class T2 extends Thread{
			@Override
			public void run() {
				while(true){
					synchronized(lock){//写入连续，加锁读取连续，不加锁不连续
						for (int i = 0; i < arr.length; i++) {
							System.out.print(arr[i]+",");
						}
						System.out.println();
					}
				}
			}
		}
		new T1().start();
		new T2().start();
	}
}
